<template>
	<view class="content">
		<u-navbar :is-mp-wx="true" :border-bottom="false">
			<template v-slot:right>
				<view class="u-flex u-m-r-40">
					<view class="u-m-r-28" @click="$u.throttle(collectClick,1000)">
						<u-image v-show="data.resumeInfo.has_fav ==0" src="/pages/pagesHome/static/collect.png"
							width="42" height="42" mode="widthFix"></u-image>
						<u-image v-show="data.resumeInfo.has_fav ==1" src="/pages/pagesHome/static/collect_1.png"
							width="42" height="42" mode="widthFix"></u-image>
					</view>
					<u-image src="/pages/pagesHome/static/share.png" width="42" height="42"
						@click="$u.throttle(shareClick,1000)" mode="widthFix"></u-image>
				</view>
			</template>
		</u-navbar>

		<view class="content_main" :class="{'u-stop-slip':data.isShare}"
			v-if="data.resumeInfo && data.resumeInfo.base_info">
			<!-- 姓名及头像 -->
			<view class="content_main_item u-flex u-row-between">
				<view class="u-white-60">
					<view class="u-font-32 u-font-weight">{{data.resumeInfo.base_info.fullname}}</view>
					<view class="u-m-t-14  " style="color: #8A8888;" v-if="data.resumeInfo.work_list.length > 0">
						{{`${data.resumeInfo.work_list[0].jobname} · ${data.resumeInfo.work_list[0].companyname}`}}
					</view>
				</view>
				<u-avatar :src="data.resumeInfo.base_info.photo_img_src" show-sex
					:sex-icon="data.resumeInfo.base_info.sex == 1 ? 'man' : 'woman'"></u-avatar>
			</view>
			<view class="content_main_item">
				<!-- 求职意向 -->
				<view class="u-flex u-row-between">
					<view>
						{{data.resumeInfo.base_info.current_text}}
					</view>
					<view class="content_main_item_ash">
						{{data.resumeInfo.base_info.refreshtime}}
					</view>
				</view>
				<!-- 经验 学历 年龄 -->
				<view class="u-flex u-m-t-30 u-font-14">
					<view class="u-flex u-m-r-40">
						<u-image src="/pages/pagesHome/static/experience.jpg" mode="widthFix" width="40"></u-image>
						<view class="u-m-l-16">
							{{data.resumeInfo.base_info.experience_text}}
						</view>
					</view>
					<view class="u-flex u-m-r-40">
						<u-image src="/pages/pagesHome/static/education.jpg" mode="widthFix" width="40"></u-image>
						<view class="u-m-l-16">
							{{data.resumeInfo.base_info.education_text}}
						</view>
					</view>
					<view class="u-flex">
						<u-image src="/pages/pagesHome/static/birthday.png" mode="widthFix" width="40"></u-image>
						<view class="u-m-l-16">
							{{data.resumeInfo.base_info.age}}岁
						</view>
					</view>
				</view>
				<!-- 个人描述 -->
				<view class="u-m-t-24 u-font-13">
					{{data.resumeInfo.base_info.specialty}}
				</view>
			</view>
			<!-- 求职期望 -->
			<view class="content_main_item" v-if="data.resumeInfo.intention_list.length > 0">
				<view class="content_main_item_tit">
					求职期望
				</view>
				<view class="u-m-t-20 content_main_item_ls" v-for="(item,index) in data.resumeInfo.intention_list"
					:key="index">
					<view class="u-flex u-row-between">
						<view class="u-font-weight">
							{{`${item.category_text}，${item.district_text}`}}
						</view>
						<view style="color:#00D075;">
							{{item.wage_text}}
						</view>
					</view>
					<view v-if="item.trade_text" class="u-m-t-10">
						{{item.trade_text}}
					</view>
				</view>
			</view>
			<!-- 工作经历 -->
			<view class="content_main_item" v-if="data.resumeInfo.work_list.length > 0">
				<view class="content_main_item_tit">
					工作经历
				</view>
				<view class="u-m-t-20 content_main_item_ls" v-for="(item,index) in data.resumeInfo.work_list"
					:key="index">
					<view class="u-flex u-row-between u-col-top">
						<view class="u-font-weight u-white-50">
							{{item.companyname}}
						</view>
						<view class="u-font-13" style="color:#8A8888;">
							{{$u.timeFormat(item.starttime,'yyyy-mm')}}至{{$u.timeFormat(item.endtime,'yyyy-mm')}}
						</view>
					</view>
					<view class="u-m-t-10">
						{{item.jobname}}
					</view>
					<view class="u-m-t-10" style="color: #8A8888;">
						{{item.duty}}
					</view>
				</view>
			</view>
			<!-- 教育经历 -->
			<view class="content_main_item" v-if="data.resumeInfo.education_list.length > 0">
				<view class="content_main_item_tit">
					教育经历
				</view>
				<view class="u-m-t-20 content_main_item_ls" v-for="(item,index) in data.resumeInfo.education_list"
					:key="index">
					<view class="u-flex u-row-between u-col-top">
						<view class="u-font-weight u-white-50">
							{{item.school}}
						</view>
						<view class="u-font-13" style="color:#8A8888;">
							{{$u.timeFormat(item.starttime,'yyyy-mm')}}至{{$u.timeFormat(item.endtime,'yyyy-mm')}}
						</view>
					</view>
					<view class="u-m-t-10">
						{{`${item.major} · ${item.education_text}`}}
					</view>
				</view>
			</view>
			<!-- 特长标签 -->
			<view class="content_main_item" v-if="data.resumeInfo.base_info.tag_text_arr.length > 0">
				<view class="content_main_item_tit">
					特长标签
				</view>
				<view class="u-flex u-flex-wrap">
					<view class="content_main_item_tag" v-for="(item,index) in data.resumeInfo.base_info.tag_text_arr"
						:key="index">
						{{item}}
					</view>
				</view>
			</view>
		</view>

		<view class="content_btm u-flex u-row-between">
			<view v-if="data.resumeInfo.show_contact == 0" class="u-m-r-80 u-flex u-flex-column"
				@click="handelResumeDownLoad">
				<u-image src="/pages/pagesHome/static/downLoadResume.png" width="40" height="40"></u-image>
				<view class="u-m-t-10">简历下载</view>
			</view>
			<view v-else class="u-m-r-80 u-flex u-flex-column" @click="toInterviewinvitation">
				<u-image src="/pages/pagesHome/static/invitation.png" width="40" height="40"></u-image>
				<view class="u-m-t-10">面试邀请</view>
			</view>
			<u-button class="u-flex-1" hover-class="none" :ripple="false" :hair-line="false"
				:custom-style="data.messageStyle" @click="$u.throttle(onlineChat, 1000)">立即联系</u-button>
		</view>

		<!-- 分享弹窗 -->
		<u-share :show="data.isShare" :src="data.src" @change="$u.throttle(shareClick,1000)"
			@close="data.isShare = false"></u-share>
		<!-- 套餐不支持下载简历时弹出 -->
		<u-modal v-model="data.showUpgradePackage" title="系统提示" content="您的套餐暂不支持下载简历，请升级套餐" show-cancel-button
			confirm-text="升级套餐" @cancel="data.showUpgradePackage =false"
			@confirm="$u.throttle(toMemberRecharge, 1000)"></u-modal>
		<!-- 提示弹窗 -->
		<u-modal v-model="data.showDirectService" title="系统提示" content="您的简历下载次数不足，请升级套餐或购买简历包" show-cancel-button
			@cancel="data.showDirectService =false" @confirm="$u.throttle(toMemberRecharge, 1000)"></u-modal>

		<!-- 聊天检测弹窗 -->
		<u-modal v-model="data.imShowstart" title="系统提示" :content="data.imStartContent" show-cancel-button
			:confirm-text="data.imStartConfirmText" @cancel="data.imShowstart =false"
			@confirm="$u.throttle(imStartConfirm, 1000)"></u-modal>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	import {
		computed,
		nextTick,
		reactive,
		watch,
	} from "vue";
	import {
		storeToRefs
	} from 'pinia'
	import {
		useUserStore
	} from '/stores/userInfo.js'
	import {
		useUserIm
	} from '/stores/userIm.js'
	const {
		isLogin
	} = storeToRefs(useUserStore())
	const IM = useUserIm()
	const data = reactive({
		id: '',
		jobid: '',
		jobname: '',
		resumeInfo: {
			has_fav: 0
		},
		messageStyle: {
			backgroundColor: '#00D075',
			color: '#fff',
			border: 'none',
			fontSize: '28rpx'
		},
		// 显示海报
		isShare: false,
		// 海报图片地址
		src: '',
		// 套餐不支持下载简历时弹出
		showUpgradePackage: false,
		// 套餐支持下载简历且余额不足时弹出
		showDirectService: false,
		// 聊天检测
		imShowstart: false,
		imStartConfirmText: '',
		imStartContent: ''
	})
	// #ifdef MP-WEIXIN
	const markers = computed(() => {
		return [{
			id: data.resumeInfo.base_info.id,
			latitude: data.resumeInfo.base_info.map_lat,
			longitude: data.resumeInfo.base_info.map_lng,
			iconPath: '/static/home/marker.png',
			width: 50
		}]
	})
	// #endif
	onLoad(({
		id,
		jobid,
		jobname
	}) => {
		data.id = id
		data.jobid = jobid
		data.jobname = jobname
		getResumeDetailInfo()
	})
	// 去面试邀请页面
	const toInterviewinvitation = () => {
		if (data.resumeInfo.base_info.audit != 1) return uni.$u.toast('该简历还未审核通过，不能继续此操作')
		uni.$u.route('/pages/pagesHome/detailPages/invitation', {
			fullname: data.resumeInfo.base_info.fullname,
			resumeid: data.resumeInfo.base_info.id
		})
	}
	// 去会员充值页面
	const toMemberRecharge = () => {
		uni.$u.route('/pages/pagesPersonal/other/privilege')
	}
	// 简历下载
	const handelResumeDownLoad = async () => {
		// 是否登录 未登录跳转登录页
		if (!isLogin.value) return uni.$u.route('/pages/login/login')
		// 判断简历是否通过审核
		if (data.resumeInfo.base_info.audit != 1) return uni.$u.toast('该简历还未审核通过，不能继续此操作')
		// 判断企业是否认证 认证正常跳转 未认证跳转认证页面
		if (uni.getStorageSync('next_code') == 50004) return uni.$u.route('pages/pagesPersonal/set/auth/auth')
		let params = {
			resume_id: data.id
		}
		let res = await uni.$u.api.getResumedownload(params)
		if (res.done == 0) {
			if (res.need_upgrade == 1) {
				data.showUpgradePackage = true
			} else {
				data.showDirectService = true
			}
			return false
		} else {
			uni.$u.toast('下载成功')
			setTimeout(() => {
				getResumeDetailInfo()
			}, 1000)
		}
		// 判断是否有下载次数

	}
	const imStartConfirm = async () => {
		uni.$u.route('pages/pagesPersonal/toolMall/package')
	}
	// 立即联系
	const onlineChat = async () => {
		// 是否登录 未登录跳转登录页
		if (!isLogin.value) return uni.$u.route('/pages/login/login')
		// 判断企业是否认证 认证正常跳转 未认证跳转认证页面
		if (uni.getStorageSync('next_code') == 50004) return uni.$u.route('pages/pagesPersonal/set/auth/auth')
		try {
			uni.showLoading({

			})
			let params = {
				token: IM.imToken,
				resumeid: data.id,
				jobid: data.jobid
			}
			let {
				companyinfo
			} = await uni.$u.api.getCompanyIndex()
			let res = await uni.$u.api.startConversation(params)
			let item = {
				jobname: data.jobname,
				nickname: data.resumeInfo.base_info.fullname,
				resumeid: data.id,
				jobid: data.jobid,
				companyid: companyinfo.id,
				chat_id: res.chatid,
				relate_uid: data.resumeInfo.base_info.uid
			}
			IM.setimChatid(res.chatid)
			if (res.next == '') {
				uni.hideLoading()
				uni.$u.route('pages/pagesMessage/detail', {
					item: decodeURIComponent(JSON.stringify(item))
				})
			}
		} catch (e) {
			uni.hideLoading()
			if (e.data.next == 'disabled') {
				uni.$u.toast(e.message)
			}
			if (e.data.next == 'bind_weixin') {
				uni.$u.toast(e.message)
				setTimeout(() => {
					uni.$u.route('pages/pagesPersonal/set/bindaccount/bindaccount')
				}, 1000)
			}
			if (e.data.next == 'pay') {
				data.imStartConfirmText = '去支付'
				data.imStartContent = e.message
				data.imShowstart = true
			}
			if (e.data.next == 'choose_job') {
				uni.$u.toast('暂不支持')
			}
		}
	}
	// 收藏职位
	const collectClick = async () => {
		// 是否登录 未登录跳转登录页
		if (!isLogin.value) return uni.$u.route('/pages/login/login')
		// 判断企业是否认证 认证正常跳转 未认证跳转认证页面
		if (uni.getStorageSync('next_code') == 50004) return uni.$u.route('pages/pagesPersonal/set/auth/auth')
		let {
			has_fav
		} = data.resumeInfo
		let params = {
			resume_id: data.id
		}
		// // 1-收藏过 0-未收藏
		if (has_fav == 0) {
			await uni.$u.api.getFavResumeAdd(params)
			data.resumeInfo.has_fav = has_fav == 1 ? 0 : 1
			uni.$u.toast('收藏成功')
		} else {
			await uni.$u.api.getFavResumeCancel(params)
			data.resumeInfo.has_fav = has_fav == 1 ? 0 : 1
			uni.$u.toast('取消收藏成功')
		}
	}
	// 分享
	const shareClick = async () => {
		try {
			let list = await uni.$u.api.getTplindexList()
			uni.showLoading()
			let params = {
				type: 'resume',
				id: data.id,
				index: uni.$u.random(1, list.length)
			}
			let res = await uni.$u.api.getPosterGenerate({
				params
			})
			uni.hideLoading()
			data.isShare = true
			data.src = res
		} catch (e) {
			uni.hideLoading()
			uni.$u.toast('生成海报失败，请联系客服')
		}
	}
	// 获取职位详情信息
	const getResumeDetailInfo = async () => {
		uni.showLoading({

		})
		let params = {
			id: data.id
		}
		let res = await uni.$u.api.getResumeDetail({
			params
		})
		data.resumeInfo = res
		uni.hideLoading()
	}
</script>

<style lang="scss" scoped>
	.content {
		color: #101010;

		&_main {
			padding-bottom: 150rpx;

			&_item {
				padding: 34rpx 50rpx;
				border-bottom: 2rpx solid rgba(187, 187, 187, 0.73);

				&_ash {
					color: #8A8888;
				}

				&_tit {
					font-size: 32rpx;
					position: relative;
					padding-left: 20rpx;
				}

				&_tit::before {
					content: ' ';
					display: block;
					position: absolute;
					top: 50%;
					left: 0;
					transform: translate(0, -50%);
					width: 10rpx;
					height: 10rpx;
					border-radius: 50%;
					background-color: #00D075;
				}

				&_tag {
					border: 2rpx solid #bbb;
					margin-right: 48rpx;
					margin-top: 22rpx;
					border-radius: 8rpx;
					color: #867F7F;
					font-size: 24rpx;
					padding: 10rpx 20rpx;
					// width: 150rpx;
					text-align: center;
				}
			}

		}

		&_btm {
			background-color: #fff;
			width: 100vw;
			z-index: 999;
			position: fixed;
			bottom: 0;
			padding: 0 36rpx;
			/* #ifndef MP-WEIXIN */
			height: 114rpx;
			/* #endif */
			/* #ifdef MP-WEIXIN */
			padding-top: 36rpx;
			padding-bottom: env(safe-area-inset-bottom);
			/* #endif */
		}
	}
</style>